/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pro.service;

import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pro.dao.FolderDao;
import com.thinkgem.jeesite.modules.pro.dao.TaskGroupDao;
import com.thinkgem.jeesite.modules.pro.entity.Folder;
import com.thinkgem.jeesite.modules.pro.entity.ProLog;
import com.thinkgem.jeesite.modules.pro.entity.TaskGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 任务组管理Service
 * @author fanqinglong
 * @version 2017-04-25
 */
@Service
@Transactional(readOnly = true)
public class TaskGroupService extends CrudService<TaskGroupDao, TaskGroup> {
	@Autowired
	private FolderDao folderDao;
	@Autowired
	private ProLogService proLogService;

	public TaskGroup get(String id) {
		return super.get(id);
	}
	
	public List<TaskGroup> findList(TaskGroup taskGroup) {
		return super.findList(taskGroup);
	}
	
	public Page<TaskGroup> findPage(Page<TaskGroup> page, TaskGroup taskGroup) {
		return super.findPage(page, taskGroup);
	}
	
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readOnly = false, rollbackFor = Exception.class)
	public void save(TaskGroup taskGroup) {
		boolean flag = StringUtils.isEmpty(taskGroup.getId());
		if (taskGroup.getIsNewRecord()){
			//如果是新增一个任务组，会新增一个和任务组名称一样的文件夹
			taskGroup.preInsert();
			dao.insert(taskGroup);
			Folder folder = new Folder();
			folder.setGroup(taskGroup);
			folder.setName(taskGroup.getName());
			folder.setProject(taskGroup.getProject());
			folder.setType("1");
			folder.preInsert();
			folderDao.insert(folder);
		}else{
			taskGroup.preUpdate();
			dao.update(taskGroup);
		}
		//项目日志
		ProLog proLog = new ProLog();
		proLog.setModule(ProLog.TASK_GROUP);
		proLog.setModuleid(taskGroup.getId());
		proLog.setData(JsonMapper.toJsonString(taskGroup));
		proLog.setType(flag?ProLog.CREATE:ProLog.UPDATE);
		proLog.setProject(taskGroup.getProject());
		proLogService.save(proLog);
	}
	
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readOnly = false, rollbackFor = Exception.class)
	public void delete(TaskGroup taskGroup) {
		super.delete(taskGroup);
		//项目日志
		ProLog proLog = new ProLog();
		proLog.setModule(ProLog.TASK_GROUP);
		proLog.setModuleid(taskGroup.getId());
		proLog.setData(JsonMapper.toJsonString(taskGroup));
		proLog.setType(ProLog.DELETE);
		proLog.setProject(taskGroup.getProject());
		proLogService.save(proLog);
	}
	
	public TaskGroup getByName(TaskGroup taskGroup){
		return dao.getByName(taskGroup);
	}
}